//
//  ViewController.m
//  1101-DopDropDownMenu-test
//
//  Created by Lang on 11/1/15.
//  Copyright © 2015 Lang. All rights reserved.
//

#import "ViewController.h"

#import "DOPDropDownMenu.h"

#import "Province.h"

@interface ViewController ()<DOPDropDownMenuDataSource, DOPDropDownMenuDelegate>
//省份
@property (nonatomic, strong) NSArray *provinces;
//城市
@property (nonatomic, strong) NSArray *cities;

@property (nonatomic, strong) DOPIndexPath *selectColRowItem;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加下拉菜单
    DOPDropDownMenu *menu =  [[DOPDropDownMenu alloc]initWithOrigin:CGPointMake(0, 64) andHeight:44];
    
    
    menu.delegate = self;
    
    menu.dataSource = self;
    
    
    //创建 menu 第一次显示,不会调用点击代理, 可以用这个手动调用
//    [menu selectDefalutIndexPath];
    
//    //选择默认显示的城市名
    DOPIndexPath *path = [DOPIndexPath indexPathWithCol:0 row:0 item:0];
    [menu selectIndexPath:path];
    

    [self.view addSubview:menu];
}

//// 可选数据源方法
///**
// *  返回 menu 有多少列 ，默认1列
// */
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu{
    return 4;
}



// ------------ 必须实现的方法
/**
 *  返回 menu 第column列有多少行
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column{
    if (column == 0) {
        return self.provinces.count;
    }else{
        return 4;
    }
    
    
}

/**
 *  返回 menu 第column列 每行title
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath{
    
    Province *provice = self.provinces[indexPath.row];
    
    if (indexPath.column == 0) {
        return provice.name;
    }else if(indexPath.column == 1){
        return @"2";
    }else if(indexPath.column == 2){
        return @"3";
    }else{
       return @"4";
    }
    
}

// 返回各省份城市的数量
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfItemsInRow:(NSInteger)row column:(NSInteger)column
{
    if (column == 0) {
        
        Province *provice = self.provinces[row];
        
        return provice.cities.count;
        
    }else{
        return 2;
    }
    
    
}


//设置省份下面的城市数据
- (NSString *)menu:(DOPDropDownMenu *)menu titleForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        //拿到每个省份的模型
        Province *provice = self.provinces[indexPath.row];
        //返回省份中城市的名称
        return provice.cities[indexPath.item];

    }else{
        return @"test";
    }
    
}




- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    
    
    
    if (indexPath.item >= 0) {
        
        if (!(self.selectColRowItem.column == indexPath.column && self.selectColRowItem.row == indexPath.row && self.selectColRowItem.item == indexPath.item)) {
        
            NSLog(@"点击了 %ld - %ld - %ld 项目",indexPath.column,indexPath.row,indexPath.item);
        }
    }else {
        
        if (!(self.selectColRowItem.column == indexPath.column && self.selectColRowItem.row == indexPath.row)) {
             NSLog(@"点击了 %ld - %ld 项目",indexPath.column,indexPath.row);
        }
        
    }
    // 记录上次点击的项目
    self.selectColRowItem = indexPath;
}



- (NSArray *)provinces{
    
    
    NSArray *provinces = [Province provinces];
    
    
    return provinces;
}

// 懒加载 index
- (DOPIndexPath *)selectColRowItem{
    if (_selectColRowItem == nil) {
        return [DOPIndexPath indexPathWithCol:0 row:0 item:0];
    }
    return _selectColRowItem;
}


@end
